package com.longge;

import java.util.Arrays;

public class ShortestToChar {

    public int[] shortestToChar(String s, char c) {

        char[] chars = s.toCharArray();
        int cNum1 = -1, cNum2 = -1;

        int[] res = new int[chars.length];
        Arrays.fill(res, -1);
        for (int i = 0; i < chars.length; i++) {
            if (c == chars[i]) cNum1 = i;
            if (cNum1 != -1) {
                res[i] = Math.abs(i - cNum1);
            }
        }

        for (int i = chars.length - 1; i >= 0; i--) {
            if (c == chars[i]) cNum2 = i;
            if (cNum2 != -1) {
                int num = Math.abs(i - cNum2);
                if (res[i] != -1) {
                    res[i] = Math.min(res[i], num);
                }else {
                    res[i] = num;
                }

            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] res = new ShortestToChar().shortestToChar("loveleetcode", 'e');
    }
}
